import java.io.IOException;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Listener;


public class chatwindow implements Runnable {

  protected Shell shell;
  protected Text messages;
  protected String user;
private String initMessage; 
  protected static ClientLogic logic;

  public chatwindow(String userID) {
  	logic = ClientLogic.getInstance();
  	logic.friends.get(userID).chatting = true;
  	logic.friends.get(userID).window = this;
      user = userID;
  }
  
  public chatwindow(String userID, String message) {
	  logic = ClientLogic.getInstance();
	  ClientLogic.friends.get(userID).chatting = true;
	  ClientLogic.friends.get(userID).window = this;
	  user = userID;
	  initMessage = message;
	  //receiveMessages(message);
	  }

        /**
         * Launch the application.
         * @param args
         */
        public static void main(String userID, String msg) {
            try {
            	logic = ClientLogic.getInstance();
                chatwindow window = new chatwindow(userID);
                window.open(userID, msg);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        /**
         * Open the window.
         * @param userID 
         * @return 
         */
        public chatwindow open(String userID, String Message) {
                Display display = Display.getDefault();
                createContents(userID, Message);
                shell.open();
                shell.layout();
                while (!shell.isDisposed()) {
                        if (!display.readAndDispatch()) {
                                display.sleep();
                        }
                }
				return this;
        }

        /**
         * Create contents of the window.
         * @param userID 
         */
        protected void createContents(final String userID, String Message) {
                shell = new Shell();
                shell.setSize(450, 520);
                shell.setText("Chat window");
                
                Label lblCurrentUser = new Label(shell, SWT.NONE);
                lblCurrentUser.setBounds(10, 10, 107, 17);
                lblCurrentUser.setText("Now talking to:");
                
                Label lblMessages = new Label(shell, SWT.NONE);
                lblMessages.setBounds(10, 33, 70, 17);
                lblMessages.setText("Messages");
                
                messages = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL | SWT.READ_ONLY);
                messages.setBounds(10, 56, 428, 277);
                messages.setText(Message);
                
                Label lblInputBox = new Label(shell, SWT.NONE);
                lblInputBox.setBounds(10, 350, 199, 17);
                lblInputBox.setText("Input Box");
                
                final Text entering = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
                entering.setBounds(10, 373, 428, 72);
                
                Label lblUserNamePlace = new Label(shell, SWT.NONE);
                lblUserNamePlace.setBounds(118, 10, 320, 17);
                lblUserNamePlace.setText(userID);
                
                Button btnSend = new Button(shell, SWT.NONE);
                btnSend.setBounds(347, 451, 91, 29);
                btnSend.setText("Send");
                btnSend.addListener(SWT.Selection, new Listener() {
                        public void handleEvent(org.eclipse.swt.widgets.Event event) {
                                String newMessage = messages.getText();
						        newMessage = newMessage + "\r\n " + logic.getUsername() + ": " + entering.getText();
						        messages.setText(newMessage);
						        try {
									logic.sendMessage(userID, entering.getText());
									entering.setText("");
								} catch (IOException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
                        }
                });

  }

  public void receiveMessages(String message){
      String newMessage = messages.getText();
      newMessage = newMessage + "\r\n" + user + ": " + message;
      messages.setText(newMessage);
  }

  	@Override
	public void run() {
	    try {
	        open(user, user + ": " + initMessage);
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	
	}
}
